package com.lh.java.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 ************************************************
 ** Module : 串行化：对象通过写出描述自己状态的数值来记述自己的过程叫串行化

**对象流：能够输入输出对象的流

**将串行化的对象通过对象流写入文件或传送到其他地方，对象流是在普通流上加了传输对象的功能，所以构造对象流时要先构造普通文件流

**注意：只有实现了Serializable接口的类才能被串行化
 ** Date: 2013-4-3  Time: 下午05:21:37 
 ** Author lh 
 ************************************************ 
 */
public class TestObjectOut {
	public static void main(String args[]) {

		ObjectOutputStream oos = null;

		try {

			oos = new ObjectOutputStream(new FileOutputStream("student.txt"));

			Student s1 = new Student("Jerry", 24);

			Student s2 = new Student("Andy", 33);

			oos.writeObject(s1);

			oos.writeObject(s2);

		} catch (Exception e) {

			e.printStackTrace();

		} finally {

			if (oos != null)

				try {

					oos.close();

				} catch (Exception e) {

					e.printStackTrace();

				}
		}
	}
}

class Student implements Serializable {

	private String name;

	private int age;

	public Student(String name, int age) {

		this.name = name;

		this.age = age;

	}

	public void greeting() {

		System.out.println("hello ,my name is " + name);

	}

	public String toString() {

		return "Student[" + name + "," + age + "]";

	}
}